package com.chengxiaoxiao.libarypartner.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chengxiaoxiao.libarypartner.R;
import com.chengxiaoxiao.libarypartner.fragment.BorrowFragment;
import com.chengxiaoxiao.libarypartner.fragment.FindFragment;
import com.chengxiaoxiao.libarypartner.fragment.IndexFragment;
import com.chengxiaoxiao.libarypartner.fragment.InfoFragment;


public class MainActivity extends Activity implements OnClickListener
{

    private RelativeLayout relatIndex;
    //private RelativeLayout relatCategory;
    private RelativeLayout relatInfo;
    private RelativeLayout relatFind;
    private RelativeLayout relatBorrow;
    private ImageView indexImage;
    private TextView indexTitle;
//    private ImageView catrgoryImage;
//    private TextView categoryTitle;
    private ImageView findImage;
    private TextView findTitle;
    private ImageView borrowImage;
    private TextView borrowTitle;
    private ImageView infoImage;
    private TextView infoTitle;





    private Fragment indexFragment;
    //private CategoryFragment categoryFragment;
    private FindFragment findFragment;
    private BorrowFragment borrowFragment;
    private InfoFragment infoFragment;


    private android.app.FragmentManager fragmentManager;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        fragmentManager = getFragmentManager();

        SetTabSelection(0);


    }

    private void SetTabSelection(int i)
    {
        //清除所有选中
        clearSelection();

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        hideFragments(transaction);
        switch (i)
        {
            case 0:
                // 当点击了首页tab时，改变控件的图片和文字颜色
                indexImage.setImageResource(R.drawable.fragment_index_selected);
                indexTitle.setTextColor(Color.parseColor("#00AA90"));
                if (indexFragment == null)
                {
                    // 如果indexFragment为空，则创建一个并添加到界面上
                    indexFragment = new IndexFragment();

                    transaction.add(R.id.frameContent, indexFragment);
                } else
                {
                    // 如果indexFragment不为空，则直接将它显示出来
                    transaction.show(indexFragment);
                }
                break;
//            case 1:
//
//                catrgoryImage.setImageResource(R.drawable.fragment_category_selected);
//                categoryTitle.setTextColor(Color.parseColor("#00AA90"));
//                if (categoryFragment == null)
//                {
//
//                    categoryFragment = new CategoryFragment();
//                    transaction.add(R.id.frameContent, categoryFragment);
//                } else
//                {
//
//                    transaction.show(categoryFragment);
//                }
//                break;

            case 1:
                // 当点击了动态tab时，改变控件的图片和文字颜色
                findImage.setImageResource(R.drawable.fragment_find_selected);
                findTitle.setTextColor(Color.parseColor("#00AA90"));
                if (findFragment == null)
                {

                    findFragment = new FindFragment();
                    transaction.add(R.id.frameContent, findFragment);
                } else
                {

                    transaction.show(findFragment);
                }

                break;
            case 2:
                // 当点击了动态tab时，改变控件的图片和文字颜色
                borrowImage.setImageResource(R.drawable.fragment_borrow_selected);
                borrowTitle.setTextColor(Color.parseColor("#00AA90"));
                if (borrowFragment == null)
                {

                    borrowFragment = new BorrowFragment();
                    transaction.add(R.id.frameContent, borrowFragment);
                } else
                {

                    transaction.show(borrowFragment);
                }

                break;
            case 3:
                // 当点击了动态tab时，改变控件的图片和文字颜色
                infoImage.setImageResource(R.drawable.fragment_info_selected);
                infoTitle.setTextColor(Color.parseColor("#00AA90"));
                if (infoFragment == null)
                {

                    infoFragment = new InfoFragment();
                    transaction.add(R.id.frameContent, infoFragment);
                } else
                {

                    transaction.show(infoFragment);
                }

                break;

        }
        transaction.commit();

    }

    private void hideFragments(FragmentTransaction transaction)
    {
        // TODO Auto-generated method stub
        if (indexFragment != null)
        {
            transaction.hide(indexFragment);
        }
//        if (categoryFragment != null)
//        {
//            transaction.hide(categoryFragment);
//        }
        if (findFragment != null)
        {
            transaction.hide(findFragment);
        }
        if (borrowFragment != null)
        {
            transaction.hide(borrowFragment);
        }
        if (infoFragment != null)
        {
            transaction.hide(infoFragment);
        }


    }

    /**
     * 清除掉所有的选中状态。
     */
    private void clearSelection()
    {
        indexImage.setImageResource(R.drawable.fragment_index_normal);
        indexTitle.setTextColor(Color.parseColor("#C0C4C9"));
//        catrgoryImage.setImageResource(R.drawable.fragment_category_normal);
//        categoryTitle.setTextColor(Color.parseColor("#C0C4C9"));
        findImage.setImageResource(R.drawable.fragment_find_normal);
        findTitle.setTextColor(Color.parseColor("#C0C4C9"));
        borrowImage.setImageResource(R.drawable.fragment_borrow_normal);
        borrowTitle.setTextColor(Color.parseColor("#C0C4C9"));
        infoImage.setImageResource(R.drawable.fragment_info_normal);
        infoTitle.setTextColor(Color.parseColor("#C0C4C9"));

    }

    private void initView()
    {
        relatIndex = (RelativeLayout) findViewById(R.id.relatIndex);
        //relatCategory = (RelativeLayout) findViewById(R.id.relatCategory);
        relatFind = (RelativeLayout) findViewById(R.id.relatFind);
        relatBorrow = (RelativeLayout) findViewById(R.id.relatBorrow);
        relatInfo = (RelativeLayout) findViewById(R.id.relatInfo);


        indexImage = (ImageView) findViewById(R.id.indexImage);
        indexTitle = (TextView) findViewById(R.id.indexTitle);

//        catrgoryImage = (ImageView) findViewById(R.id.categoryImage);
//        categoryTitle = (TextView) findViewById(R.id.categoryTitle);

        findImage = (ImageView) findViewById(R.id.findImage);
        findTitle = (TextView) findViewById(R.id.findTitle);

        borrowImage = (ImageView) findViewById(R.id.borrowImage);
        borrowTitle = (TextView) findViewById(R.id.borrowTitle);

        infoImage = (ImageView) findViewById(R.id.infoImage);
        infoTitle = (TextView) findViewById(R.id.infoTitle);

        relatIndex.setOnClickListener(this);
        //relatCategory.setOnClickListener(this);
        relatFind.setOnClickListener(this);
        relatBorrow.setOnClickListener(this);
        relatInfo.setOnClickListener(this);


    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.relatIndex:
                // 当点击了消息tab时，选中第1个tab
                SetTabSelection(0);
                break;

            case R.id.relatFind:
                SetTabSelection(1);
                break;
            case R.id.relatBorrow:
                SetTabSelection(2);
                break;
            case R.id.relatInfo:
                SetTabSelection(3);
                break;

            default:
                break;
        }
    }
}
